package com.rongzhe.house.presenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.AppointmentTo;
import com.rongzhe.house.entity.Dictionary;
import com.rongzhe.house.entity.voo.HouseDetail;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.DictionaryManager;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.DateChooseActivity;
import com.rongzhe.house.ui.activity.LookForHouseActivity;
import com.rongzhe.house.ui.activity.LookForHouseOrderSuccessActivity;
import com.rongzhe.house.utils.StringUtils;
import com.rongzhe.house.utils.UIUtils;

import java.util.List;

/**
 * Created by huang on 2018/1/3.
 */

public class LookForHousePresenter extends BasePresenter {
    public static final String EXTRA_HOUSE_ID = "extra_house_id";
    public static final String EXTRA_HOUSE_TITLE = "extra_house_title";
    private static final int REQUEST_CODE_CHOOSE_DATE = 1;
    private int mHouseId = -1;
    private AppointmentTo mAppointmentTo = new AppointmentTo();

    public LookForHousePresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    public void setContact(String contact) {
        mAppointmentTo.setContacts(contact);
    }

    public void setDate(String date) {
        mAppointmentTo.setContactTime(date);
    }

    public void setSex(String sex) {
        mAppointmentTo.setContactsSex(sex);
    }

    public void setContactTimeOption(String timeOption) {
        mAppointmentTo.setContactTimeOption(timeOption);
    }

    public void setContactNumber(String number) {
        mAppointmentTo.setContactsMobile(number);
    }

    public void setMessage(String message) {
        mAppointmentTo.setMessage(message);
    }

    public void onChooseDateClick() {
        Intent intent = new Intent(mContext, DateChooseActivity.class);
        getUiControlInterface().launchActivityForResult(REQUEST_CODE_CHOOSE_DATE, intent);
    }

    @Override
    public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_CHOOSE_DATE && resultCode == Activity.RESULT_OK) {
            String time = data.getStringExtra(DateChooseActivity.EXTRA_TIME);
            setDate(time);
            ((LookForHouseActivity) getUiControlInterface()).setShowingDate(time);
            return true;
        }
        return false;
    }

    @Override
    public void start(Intent intent) {
        HouseDetail houseDetail = HouseManager.getInstance().getCurrentHouseDetail();
        if (houseDetail == null) {
            getUiControlInterface().finish();
            return;
        }
        ((LookForHouseActivity) getUiControlInterface()).setHouseDetailInfo(houseDetail);
        loadTimeSlotInfo();
        mAppointmentTo.setHouseId(houseDetail.getId());
    }

    private void loadTimeSlotInfo() {
        DictionaryManager.getInstance().getDictionaryByType(
                DictionaryManager.DICTIONARY_CONTACT_TIME_OPTION,
                new DataListener<List<Dictionary>>() {
                    @Override
                    public void onSuccess(List<Dictionary> data, String message) {
                        ((LookForHouseActivity) getUiControlInterface()).initTimeSlot(data);
                        if (data != null && data.size() > 0) {
                            mAppointmentTo.setContactTimeOption(data.get(0).getCode());
                        }
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {

                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    private boolean checkForData() {
        if(TextUtils.isEmpty(mAppointmentTo.getContacts()) || mAppointmentTo.getContacts().length() < 2) {
            getUiControlInterface().showToast(getString(R.string.input_real_name));
            return false;
        }
        if (!StringUtils.isChinaPhoneLegal(mAppointmentTo.getContactsMobile())) {
            getUiControlInterface().showToast("请输入正确的手机号码!");
            return false;
        }
        if (TextUtils.isEmpty(mAppointmentTo.getContactTime())) {
            getUiControlInterface().showToast("请选择看房时间!");
            return false;
        }
        return true;
    }

    public void doSubmit() {
        if(!checkForData()) {
            return;
        }
        UserManager.getInstance().houseAppointment(mAppointmentTo, new DataListener<Object>() {
            @Override
            public void onSuccess(Object data, String message) {
//                getUiControlInterface().showAlert(UIUtils.createHintMessageWithClick(mContext,
//                        getString(R.string.hint),
//                        getString(R.string.appointment_success),
//                        getString(android.R.string.ok),
//                        new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                getUiControlInterface().finish();
//                            }
//                        }));
                Intent intent = new Intent(mContext, LookForHouseOrderSuccessActivity.class);
                getUiControlInterface().launchActivity(true, intent);
                getUiControlInterface().finish();
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {

            }
        });
    }
}
